﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Amoo.WebControls
{
    public partial class ColunaDireitaSite : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            MontarListaUltimasAtualizacoes();
            
            if ((Code.Entity.Usuario)Session["userSite"] != null)
            {
                HabilitaUsuario(true);
            }
            else
            {
                HabilitaUsuario(false);
            }

        }

        private void MontarListaUltimasAtualizacoes()
        {
            Code.Data.Comunidade oComunidade = new Amoo.Code.Data.Comunidade();
            Code.Data.Noticia oNoticia = new Amoo.Code.Data.Noticia();
            //List<Code.Entity.Noticia> returnValNoticia = oNoticia.GetForHome();

            string strHTML = "";
            List<string> listString = new List<string>();

            foreach (Code.Entity.Comunidade eComunidade in oComunidade.GetForHome())
            {
                strHTML += "<table cellpadding='5' cellspacing='0' border=0 width=100%>";
                strHTML += "    <tr>";
                strHTML += "        <td width='99%' align='left' valign='top'>";
                strHTML += "            <table>";
                strHTML += "                <tr>";
                strHTML += "                    <td width='10%' valign=top><img src='/images/comunidade.jpg' width=30px height=30px></td>";
                strHTML += "                    <td valign='top'>";
                strHTML += "                        <a href='/Visualizar/Comunidade.aspx?idComunidade=" + eComunidade.idComunidade + "'><font class='subtitle'>" + eComunidade.dsNomeComunidade + "</font></a>";
                strHTML += "                    </td>";
                strHTML += "                </tr>";
                strHTML += "            </table>";
                strHTML += "        </td>";
                strHTML += "    </tr>";
                strHTML += "</table>";
            }

            //foreach (Code.Entity.Noticia eNoticia in oNoticia.GetForHome())
            //{
            //    strHTML = "<table cellpadding='5' cellspacing='0' border=0 width=100%>";
            //    strHTML += "    <tr>";
            //    strHTML += "        <td width='99%' align='left' valign='top'>";
            //    strHTML += "            <table border=0>";
            //    strHTML += "                <tr>";
            //    strHTML += "                    <td width='10%' valign=top><img src='/images/icon_news_32.png' width=30px height=30px></td>";
            //    strHTML += "                    <td valign='top'>";
            //    strHTML += "                        <a href='/Visualizar/DetalheNoticia.aspx?idNoticia=" + eNoticia.idNoticia + "'><font class='subtitle'>" + eNoticia.dsTitulo + "</font></a>";
            //    //strHTML += "                        <a href='/Visualizar/DetalheNoticia.aspx?idNoticia=" + eNoticia.idNoticia + "'><p class='text4'>" + (eNoticia.dsHtml.Length > 300 ? eNoticia.dsHtml.Substring(0, 300) : eNoticia.dsHtml.Substring(0, eNoticia.dsHtml.Length)) + "</p></a>";
            //    strHTML += "                    </td>";
            //    strHTML += "                </tr>";
            //    strHTML += "            </table>";
            //    strHTML += "        </td>";
            //    strHTML += "    </tr>";
            //    strHTML += "</table>";
            //    listString.Add(strHTML);
            //}

            //foreach (string strHTMLDone in listString)
            //{
            //    dvUltimasNovidades.InnerHtml += strHTMLDone;
            //}

            dvUltimasNovidades.InnerHtml = strHTML;


        }

        protected void Unnamed1_Click(object sender, ImageClickEventArgs e)
        {
            Code.Business.Usuario oUsuario = new Amoo.Code.Business.Usuario();
            Code.Entity.Usuario eUsuario = new Amoo.Code.Entity.Usuario()
            {
                dsEmailAmoo = txtNome.Text,
                dsSenha = txtSenha.Text
            };

            eUsuario = oUsuario.Login(eUsuario);

            if (eUsuario != null)
            {
                Session["userSite"] = eUsuario;
                HabilitaUsuario(true);
                Response.Redirect("/");
            }
            else
            {
                HabilitaUsuario(false);
                ShowWindowAlert("Usuário ou senha inválidos!");
            }


        }

        private void HabilitaUsuario(bool pAction)
        {
            if (pAction)
            {
                dvBoxLogin.Visible = false;
                dvUsuarioLogado.Visible = true;
                lblNomeusuario.Text = ((Code.Entity.Usuario)Session["userSite"]).dsNome;

                Code.Data.Usuario oUsuario = new Amoo.Code.Data.Usuario();
                List<int> oReturnValue = new List<int>();

                oReturnValue = oUsuario.GetUsuariosFeatures((Code.Entity.Usuario)Session["userSite"]);

                lblQtdeComunidades.Text = oReturnValue[0].ToString();
                lblQtdeNoticias.Text = oReturnValue[1].ToString();
                lblQtdeBanners.Text = oReturnValue[2].ToString();

            }
            else
            {
                dvBoxLogin.Visible = true;
                dvUsuarioLogado.Visible = false;
            }
        }

        protected void btnSair_Click(object sender, ImageClickEventArgs e)
        {
            Session["userSite"] = null;
            Response.Redirect("/");
        }

        protected void ShowWindowAlert(string pMessage)
        {

            string strHtml = "";

            strHtml += "<div id='dvWindowAlert' style='background-color: #999999; position:absolute; top:0px; left:0px; width:100%; height:100%; filter:alpha(opacity=40);'>";
            strHtml += "    <div style='border: 2px solid #ccffcc; position:absolute; top:35%; left:30%;; background-color: #FFFFFF; width:500px; height:100px;  font-family: Arial, Helvetica, sans-serif; font-size: 17px; color: #000000;'>";
            strHtml += "        <div style='position:absolute; background-color:#ccffcc; width:100%; height:20px;'>";
            strHtml += "            <div style='position:absolute; left:95%'><img src='/Images/close.png' title='Fechar a janela' style='cursor:hand;' onclick='closeWindowAlert()'/></div>";
            strHtml += "        </div>";
            strHtml += "        <div style='position:absolute; top:35%; left:10px'><img src='/Images/Erro.jpg' width='40px' height='40px'></div>";
            strHtml += "        <div style='position:absolute; top:25%; left:70px'>" + pMessage + "</div>";
            strHtml += "    </div>";
            strHtml += "<script language=javascript>";
            strHtml += "    function closeWindowAlert()";
            strHtml += "    {";
            strHtml += "        document.getElementById('dvWindowAlert').style.visibility='hidden';";
            strHtml += "    }";
            strHtml += "</script>";
            strHtml += "</div>";

            Page.RegisterStartupScript("WindowAlert", strHtml);
        }
    }
}